Ext.define('CDR.view.recovery.TariffRecovery', {
	extend: 'Ext.Container',
	alias: 'widget.tariff-call-recovery',
	id: 'x-cdr-tariff-recovery',
	title: 'Recovery',
	layout: 'border',
	fileName: '',
	border: false,
	initComponent: function(){
		var me = this;
		me.items = [{
			xtype: 'panel',
			region: 'center',
			border: false,
			layout: 'fit',
			title: 'Data',
			tbar: ['->',{
				text: 'recovery',
				name: 'recovery',
				iconCls: 'icon-refresh',
				handler: function(){
					Ext.getBody().mask("Waiting...");
					Ext.Ajax.request({
						url: 'recoveryProcess.html',
						method: 'GET',
						params: {
							fileName: me.fileName
						},
						success: function(response){
							Ext.getBody().unmask();
							var data = Ext.decode(response.responseText);
							if(data.success) {
								alert('Recovery success');
							} else {
								alert('Recovery fail');
							}
						},
						failure:function(){
							Ext.getBody().unmask();	
							alert('Recovery fail');
						}
					});
				}
			}],
			items: [{
				autoScroll: true,
				xtype: 'gridpanel',
				id: 'x-tariff-call-record-rawdata',
				border: false,
				store: Ext.create('CDR.store.recovery.RecoveryFileDataStore'),
				columns: [{
                    xtype: 'rownumberer',
                    width: 30
                },{
					header: 'Pbx Id',  
					dataIndex:'pbxId', 
					menuDisabled: true,
					sortable: false,
					width: 70
				} ,{
					header: 'Raw log',  
					dataIndex:'rowLog', 
					menuDisabled: true,
					sortable: false,
					flex: 1
				}]
			}]
		}, {
			xtype: 'panel',
			title: 'Config',
			width: '100%',
			split: true,
			collapsible: true,
			region: 'west',
			layout:'border',
			items: [{
				xtype:'form',
				border: false,
				region:'north',
				bodyPadding: 20,
				height: '20%',
				defaults:{
					xtype: 'textfield',
					labelWidth: 250
				},
				items: [{
					xtype: 'hidden',
					name: 'id'
				},{
					fieldLabel: 'Email to send when has error',
					name: 'email',
					vtype: 'email',
					anchor: '70%'
				}, {
					fieldLabel: 'Interval time since the last sent (Minutes)',
					name: 'interval',
					xtype: 'numberfield',
					value: 20
				}],
				buttonAlign: 'center',
				buttons: [{
					text: 'Save',
					handler: function(bnt){
						var form = bnt.up('form').getForm();
						form.submit({
				            url: 'saveServerConfig.html',
				            success: function (form, action) {
				                var data = action.result;
				                if(data.success && data.result){
				                	bnt.up('form').down('field[name=id]').setValue(data.result.id);
				                	Ext.Msg.alert("Error", 'Save is ok');
				                }
				            },
				            failure: function (form, action) {
				                Ext.Msg.alert("Error", 'Save is fail');
				            }
				        });
					}
				}]
			},{
				xtype:'panel',
				region:'center',
				title: 'Recovery list',
				border: false,
				layout: 'fit',
				items: [{
					xtype: 'gridpanel',
					border: false,
					store: Ext.create('CDR.store.recovery.RecoveryFileStore'),
					viewConfig: { 
				        stripeRows: false, 
				        getRowClass: function(record) { 
				            return (record.get('recovery') == true) ? 'discount-row' : ''; 
				        } 
				    }, 
					columns: [{
						header: 'Data',  
						dataIndex:'fileName', 
						menuDisabled: true,
						sortable: false,
						flex: 1
					},{
						xtype:'actioncolumn',
                        items:[{
                            icon: 'resources/app/images/delete.png',
                            tooltip: 'Delete',
                            handler: function (grid, rowIndex, colIndex, item, e) {
                            	e.stopEvent();
                            	var rc = grid.getStore().getAt(rowIndex);
                            	var delFunc  = function(){
                            		Ext.Ajax.request({
										url: 'deleteRecovery.html',
										method: 'GET',
										params: {
											fileName: rc.data.fileName
										},
										success: function(response){
											var data = Ext.decode(response.responseText);
											if(data.success) {
												 grid.getStore().removeAt(rowIndex);
											} else {
												Ext.Msg.alert('Can not remove this file');
											}
										},
										failure:function(){
											Ext.Msg.alert('Can not remove this file');
										}
									});
                            	};
                            	
                            	if(rc.data.recovery === true){
                            		Ext.Msg.confirm("Info", "Do you want to remove this file", function(button) {
					                    if (button == 'yes') {
					                    	delFunc();
					                    }
					                });
                            	} else {
                            		Ext.Msg.confirm("Warning", "This file still not recovery. Do you want to remove this file ?", function(button) {
					                    if (button == 'yes') {
					                    	delFunc()
					                    }
					                });
                            	}
                            	return false;
                            }
                        }],
                        width: 30
					}],
					listeners: {
						itemclick: function( gr, record, item, index){
							var dataGrid = Ext.getCmp('x-tariff-call-record-rawdata');
							if(dataGrid){
								if(record.data.recovery === true){
									dataGrid.up('panel').down('toolbar button[name=recovery]').disable(true);
								} else {
									dataGrid.up('panel').down('toolbar button[name=recovery]').enable(true);
								}
								dataGrid.store.removeAll();
								me.fileName = record.data.fileName;
								dataGrid.store.load({params:{
									fileName: record.data.fileName
								}});
							}
						}
					}
				}]
			}]
		}];
		me.callParent(arguments);
		
	},
	listeners: {
		afterrender: function(comp){
			Ext.Ajax.request({
				url: 'serverConfig.html',
				method: 'GET',
				success: function(response){
					var data = Ext.decode(response.responseText);
					if(data.success && data.result) {
						comp.down('field[name=id]').setValue(data.result.id);
						comp.down('field[name=email]').setValue(data.result.email);
						comp.down('field[name=interval]').setValue(data.result.interval);
					}
				}
			});
		}
	}
	
});